package lab2;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class ReadSalaryUtil {
    public static double getSalary(String salaryName, String nodeName){
        try {
            double salary = 0.0;
            File file = new File("src\\salary.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);

            NodeList nodeList = doc.getElementsByTagName(nodeName);

            for (int j = 0; j < nodeList.getLength(); ++j) {
                Node node = nodeList.item(j);
                if (node.getAttributes().getNamedItem("name").getNodeValue().equalsIgnoreCase(salaryName)) {
                    salary = Double.parseDouble(node.getAttributes().getNamedItem("value").getNodeValue());
                }
            }
            return salary;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}